package com.example.muasic.service;

import android.app.Service;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.ImageView;

import com.example.muasic.info.Info;
import com.example.muasic.activity.PlayingActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by so on 2018/1/26.
 */

public class MusicService extends Service {

    private final static String TAG = "sorrower";
    private static int duration;
    private static int playMode=0;
    private static int NowMusic=-1;
    private static List<Info> NowList=new ArrayList<>();
    private static List<Bitmap> NowBitList=new ArrayList<>();
    private static ImageView fav;
    private static final boolean keepTrue = true;
    private static MediaPlayer mediaPlayer;


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        //3. 返回自定义类MyBinder对象
        return new MyBinder();
    }

    @Override
    public void onCreate() {
        mediaPlayer = new MediaPlayer();
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
        super.onDestroy();
    }

    /**
     * 类MyBinder继承Binder实现接口IService
     */
    private class MyBinder extends Binder implements IService {
        //2. 定义类MyBinder继承Binder实现接口IService中的函数
        @Override
        public void callPlayMusic(String path) {
            [(path);
        }

        @Override
        public void callPauseMusic() {
            pauseMusic();
        }

        @Override
        public void callConMusic() {
            conMusic();
        }
        @Override
        public void callgetplaymode(int mode) {
            getPlayMode(mode);
        }

        @Override
        public void callgetNowmusicList(List<Info> list) {
            NowMusicList(list);
        }
        @Override
        public long callgetNowMusicId() {
            long Nowm = getNowMusicId();
            return Nowm;
        }
        @Override
        public void callNowMusic(int path1) {
            NowMusic(path1);
        }
        @Override
        public int callgetNowMusic() {
            int Now = getNowMusic();
            return Now;
        }

        @Override
        public void callsetNowBitList(List<Bitmap> List) {
            setNowBitList(List);
        }
        @Override
        public List<Bitmap> callgetNowBitList() {
            List<Bitmap> NowBit = new ArrayList<>();
            NowBit = getNowBitList();
            return NowBit;
        }

        @Override
        public List<Info> callNowMusicList() {
            List<Info> NowMuList = new ArrayList<>();
                  NowMuList= getNowMusicList();
            return NowMuList;
        }

        @Override
        public int callNowPlayMode() {
            int Nowmode = NowPlayMode();
            return Nowmode;
        }

        @Override
        public MediaPlayer callgetMediaPlayer() {
            MediaPlayer media= getMediaPlayer();
            return media;
        }

        @Override
        public void callDestroyMusic() {
            onDestroy();
        }

        @Override
        public void callSeekToPos(int pos) {
            seekToPos(pos);
        }
    }

    /**
     * 播放音乐
     *
     * @param
     */
    public void getPlayMode(int Mode){
        playMode=Mode;
    }

    public int NowPlayMode(){
        return playMode;
    }

    public MediaPlayer getMediaPlayer(){
        return mediaPlayer;
    }

    public void NowMusic(int path1){
        NowMusic=path1;
    }

    public int getNowMusic(){
        return NowMusic;
    }

    public static void setNowBitList(List<Bitmap> Bitlist){
        NowBitList=Bitlist;
    }

    public List<Bitmap> getNowBitList(){
        return NowBitList;
    }


    public void NowMusicList(List<Info> list){
        NowList=list;
    }

    public List<Info> getNowMusicList(){
        return NowList;
    }
    public long getNowMusicId(){
        long NowMusicId;
        if(NowMusic<0||NowMusic>=NowList.size()){
            NowMusicId=0;
        }else {
            NowMusicId = NowList.get(NowMusic).getSongId();
        }
        return NowMusicId;
    }
    public static void playMusic(String path) {
        Log.i(TAG, "playMusic");
        try {
            if (mediaPlayer != null) {
                mediaPlayer.stop();
            }
            mediaPlayer.reset();
            mediaPlayer.setDataSource(path);
            mediaPlayer.prepare();
            mediaPlayer.start();

            updateSeekBar();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 更新SeekBar
     */
    private static void updateSeekBar() {
        //获取总时长
        duration = mediaPlayer.getDuration();
        //开启线程发送数据
        new Thread() {
            @Override
            public void run() {

                while (keepTrue) {

                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    int currentPosition = mediaPlayer.getCurrentPosition();

                    //发送数据给activity
                    Message message = Message.obtain();
                    Bundle bundle = new Bundle();
                    bundle.putInt("duration", duration);
                    bundle.putInt("currentPosition", currentPosition);
                    message.setData(bundle);

                    PlayingActivity.handler.sendMessage(message);

                }

            }
        }.start();
    }

    /**
     * 暂停播放音乐
     */
    public void pauseMusic() {
        Log.i(TAG, "pauseMusic");
        if (mediaPlayer.isPlaying()) {
            mediaPlayer.pause();
        }
    }

    /**
     * 继续播放音乐
     */
    public void conMusic() {
        Log.i(TAG, "conMusic");
        mediaPlayer.start();
    }

    /**
     * 设置SeekBar位置
     *
     * @param pos 当前位置
     */
    public void seekToPos(int pos) {
        mediaPlayer.seekTo(pos);
    }
}
